package com.pi.shisan.service.util;

import de.quippy.javamod.mixer.dsp.DspProcessorCallBack;

import java.util.function.Function;

public class DspProcessorCallBackFactory {
    
    private static Function<ComponentManager.MusicEffectBaseComponent, DspProcessorCallBack> DSP_PROCESSOR = musicEffectComponents -> new DspProcessorCallBack() {
        private FastFFT fft = new FastFFT(2048);
        @Override
        public void currentSampleChanged(float[] leftSample, float[] rightSample) {
            if (musicEffectComponents != null) {
                if (musicEffectComponents.getLeftMusicEffectComponent() != null && leftSample != null) {
                    fft.calculate(leftSample);
                    musicEffectComponents.getLeftMusicEffectComponent().setVal(fft.getMax());
                }
                if (musicEffectComponents.getRightMusicEffectComponent() != null && rightSample != null) {
                    fft.calculate(rightSample);
                    musicEffectComponents.getRightMusicEffectComponent().setVal(fft.getMax());
                }
            }
        }
    };
    
    public static void setDspProcessor(Function<ComponentManager.MusicEffectBaseComponent, DspProcessorCallBack> dspProcessor) {
        DSP_PROCESSOR = dspProcessor;
    }

    public static DspProcessorCallBack getDspProcessorCallback(ComponentManager.MusicEffectBaseComponent musicEffectComponents) {
        final Function<ComponentManager.MusicEffectBaseComponent, DspProcessorCallBack> f = DSP_PROCESSOR;
        if (f != null) {
            return f.apply(musicEffectComponents);
        }
        return null;
    }
    
}
